package tv.huan.web.controller.tencent;

import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.models.auth.In;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import tv.huan.common.annotation.Log;
import tv.huan.common.core.controller.BaseController;
import tv.huan.common.core.domain.AjaxResult;
import tv.huan.common.core.page.TableDataInfo;
import tv.huan.common.enums.BusinessType;
import tv.huan.common.utils.DateUtils;
import tv.huan.common.utils.poi.ExcelUtil;
import tv.huan.system.service.ISysDictDataService;
import tv.huan.tencent.domain.CardConsumeLog;
import tv.huan.tencent.domain.CardPool;
import tv.huan.tencent.service.ICardConsumeLogService;
import tv.huan.tencent.service.ICardPoolService;
import tv.huan.tencent.utils.AESCoder;

/**
 * 消费记录Controller
 * 
 * @author admin
 * @date 2020-12-23
 */
@Controller
@RequestMapping("/tencent/cardConsumeLog")
public class CardConsumeLogController extends BaseController
{
    private String prefix = "tencent/cardConsumeLog";

    @Autowired
    private ICardConsumeLogService cardConsumeLogService;

    @Autowired
    private ICardPoolService cardPoolService;

    @RequiresPermissions("tencent:cardConsumeLog:view")
    @GetMapping()
    public String cardConsumeLog(ModelMap mmap)
    {
        List<CardPool> cardPoolList = cardPoolService.selectCardPoolList(null);
        mmap.put("cardPoolList",cardPoolList);
        mmap.put("cardPool",new CardPool());
        return prefix + "/cardConsumeLog";
    }

    /**
     * 查询消费记录列表
     */
    @RequiresPermissions("tencent:cardConsumeLog:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CardConsumeLog cardConsumeLog)
    {
        startPage();
        List<CardConsumeLog> list = cardConsumeLogService.selectCardConsumeLogList(cardConsumeLog);
        return getDataTable(list);
    }

    /**
     * 导出消费记录列表
     */
    @RequiresPermissions("tencent:cardConsumeLog:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CardConsumeLog cardConsumeLog)
    {
        List<CardConsumeLog> list = cardConsumeLogService.selectCardConsumeLogList(cardConsumeLog);
        ExcelUtil<CardConsumeLog> util = new ExcelUtil<CardConsumeLog>(CardConsumeLog.class);
        return util.exportExcel(list, "cardConsumeLog");
    }

    /**
     * 新增消费记录
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        List<CardPool> cardPoolList = cardPoolService.selectCardPoolList(null);
        mmap.put("cardPoolList",cardPoolList);
        mmap.put("cardPool",new CardPool());
        return prefix + "/add";
    }

    /**
     * 新增保存消费记录
     */
    @RequiresPermissions("tencent:cardConsumeLog:add")
    @Log(title = "消费记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CardConsumeLog cardConsumeLog)
    {
        return toAjax(cardConsumeLogService.insertCardConsumeLog(cardConsumeLog));
    }

    /**
     * 修改消费记录
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        CardConsumeLog cardConsumeLog = cardConsumeLogService.selectCardConsumeLogById(id);
        mmap.put("cardConsumeLog", cardConsumeLog);
        return prefix + "/edit";
    }

    /**
     * 修改保存消费记录
     */
    @RequiresPermissions("tencent:cardConsumeLog:edit")
    @Log(title = "消费记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CardConsumeLog cardConsumeLog)
    {
        return toAjax(cardConsumeLogService.updateCardConsumeLog(cardConsumeLog));
    }

    /**
     * 删除消费记录
     */
    @RequiresPermissions("tencent:cardConsumeLog:remove")
    @Log(title = "消费记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(cardConsumeLogService.deleteCardConsumeLogByIds(ids));
    }

    /**
     * 接受上报过来的消费数据
     */
    @PostMapping( "/reportData")
    @ResponseBody
    public String reportData(String reportInfo) {
        JSONObject result = new JSONObject();
        String content;
        try {
            content = new String(AESCoder.decrypt(reportInfo, "F51riKYgKK8PRrt+5IC6CQ=="),"UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
            result.put("msg","无权限");
            result.put("code","-1");
            return result.toString();
        }
        JSONObject jsonObject = JSON.parseObject(content);
        try {
            //卡池所属
            String cardScope = jsonObject.getString("cardScope");
            //卡池类型
            String cardType = jsonObject.getString("cardType");
            //渠道
            String channel = jsonObject.getString("channel");
            //订购数
            int orderCount = jsonObject.getIntValue("orderCount");
            //实际开通数
            int openCount = jsonObject.getIntValue("openCount");
            //消费时间
            String syncTime = jsonObject.getString("syncTime");
            Date syncDate = DateUtils.parseDate(syncTime);
            CardPool cp = cardPoolService.selectCardPoolByScopeAndType(Integer.parseInt(cardScope),Integer.parseInt(cardType));
            if (null != cp) {
                CardConsumeLog ccl = new CardConsumeLog();
                ccl.setCardId(cp.getCardId());
                ccl.setConsumeChannel(Integer.parseInt(channel));
                ccl.setOrderCount(orderCount);
                ccl.setOpenCount(openCount);
                ccl.setSyncTime(syncDate);
                cardConsumeLogService.insertCardConsumeLog(ccl);
                result.put("msg","上报成功");
                result.put("code","0");
            } else {
                result.put("msg","无对应卡池");
                result.put("code","-1");
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
            result.put("msg","上报格式错误");
            result.put("code","-1");
        }
        return result.toString();
    }
}
